package Thread;

class Thread1 extends Thread{
    private String name;
    public Thread1(String name){
        super(name);//调用父类的方法/属性
        this.name=name;
    }
    public void run(){
        System.out.println(Thread.currentThread().getName()+"线程开始运行");
        for(int i=0;i<5;i++)
        {
            System.out.println("子线程"+name+"运行:"+i);
            try {
                sleep((int) Math.random()*10);
        }
            catch (InterruptedException e ){
                e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
        }
        }
        System.out.println(Thread.currentThread().getName()+"线程运行结束！");
    }
}

public class Main {
    public static void main(String[] args)
    {
        System.out.println(Thread.currentThread().getName()+"主线程开始运行");
        Thread1 thread1 = new Thread1("A");
        Thread1 thread2 = new Thread1("B");
        thread1.start();
        thread2.start();
        try{
            thread1.join(); //加入join主线程会等子线程都结束了才结束
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        try{
            thread2.join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"主线程运行结束");
    }

}
